<?php


namespace App\Http\Controllers\Admin;


use App\Http\Requests\CoinsCodingCreateRequest;
use App\Http\Requests\CoinsCodingJuniorCreateRequest;
use App\Http\Requests\CoinsCodingJuniorUpdateRequest;
use App\Http\Requests\CoinsCodingOnoffRequest;
use App\Http\Requests\CoinsCodingRequest;
use App\Http\Resources\HomeResource;
use App\Http\Services\Admin\CoinsTypeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CoinsCodingController
{
    /**
     * Description：编码列表
     * @param Request $request
     * @param CoinsTypeService $service
     * @return |null
     */
    public function list(Request $request, CoinsTypeService $service)
    {
        $service->make($request->input());
        return $service->get();
    }

    /**
     * Description：创建编码
     * @param CoinsCodingCreateRequest $request
     * @param CoinsTypeService $service
     * @return mixed
     */
    public function create(CoinsCodingCreateRequest $request, CoinsTypeService $service)
    {
        return DB::transaction(function () use ($request, $service) {
            return $service->create($request->input());
        });
    }

    /**
     * Description：红包编码开启使用
     * @param CoinsCodingRequest $request
     * @param CoinsTypeService $service
     * @return mixed
     */
    public function onoff(CoinsCodingOnoffRequest $request, CoinsTypeService $service)
    {
        return DB::transaction(function () use ($request, $service) {
            return $service->onoff($request->input());
        });

    }

    /**
     * Description：二级红包编码详情-列表
     * @param Request $request
     * @param CoinsTypeService $service
     * @param $parent_code
     * @return mixed
     */
    public function junior_list(Request $request, CoinsTypeService $service, $parent_code)
    {
        $params = $request->input('params',[]);
        array_push($params,[['parent_code', '=', $parent_code]]);
        $request->offsetSet('params',$params);

        return $service->make($request->input())->get();
    }


    /**
     * Description：创建二级红包编码详情
     * @param CoinsCodingJuniorCreateRequest $request
     * @param CoinsTypeService $service
     * @return mixed
     */
    public function junior_create(CoinsCodingJuniorCreateRequest $request, CoinsTypeService $service)
    {
        return DB::transaction(function () use ($request, $service) {
            return $service->junior_create($request->input());
        });
    }

    /**
     * Description：修改二级红包编码详情
     * @param CoinsCodingJuniorUpdateRequest $request
     * @param CoinsTypeService $service
     * @return mixed
     */
    public function junior_update(CoinsCodingJuniorUpdateRequest $request, CoinsTypeService $service)
    {
        return DB::transaction(function () use ($request, $service) {
            return $service->junior_update($request->input());
        });
    }

    /**
     * Description：红包编码开启使用
     * @param CoinsCodingRequest $request
     * @param CoinsTypeService $service
     * @return mixed
     */
    public function junior_onoff(CoinsCodingOnoffRequest $request, CoinsTypeService $service)
    {
        return DB::transaction(function () use ($request, $service) {
            return $service->onoff($request->input());
        });

    }

    /**
     * Description：红包编码关闭使用
     * @param CoinsCodingRequest $request
     * @param CoinsTypeService $service
     * @return mixed
     */
    public function junior_close(CoinsCodingRequest $request, CoinsTypeService $service)
    {
        return DB::transaction(function () use ($request, $service) {
            return $service->close($request->input('id'));
        });
    }

}
